// EditWnd.cpp : implementation file
//

#include "stdafx.h"
#include "ISISMARC.h"
#include "ISISMARCView.h"
#include "EditWnd.h"
#include "mensaje.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CIsismarcView * vpView;

/////////////////////////////////////////////////////////////////////////////
// CEditWnd

CEditWnd::CEditWnd(CEditGrid * p)
{
	pgrilla = p;
	hubo_sub = false;

	CMDIFrameWnd *pFrame = 
				 (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

	// Get the active MDI child window.
	CMDIChildWnd *pChild = 
				 (CMDIChildWnd *) pFrame->GetActiveFrame();

	// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

	// Get the active view attached to the active MDI child
	// window.
	vpView = (CIsismarcView *) pChild->GetActiveView();

}

CEditWnd::~CEditWnd()
{
}


BEGIN_MESSAGE_MAP(CEditWnd, CEdit)
	//{{AFX_MSG_MAP(CEditWnd)
	ON_WM_KEYUP()
	ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditWnd message handlers

void CEditWnd::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	m_bBlock = true;
	int row = pgrilla->GetRow();

	switch ( nChar )
	{
	case VK_RETURN:	//cambia datos
	{
		CString tex;
		GetWindowText(tex);

		pgrilla->OnEnter(tex);
		SetWindowText("");
		ShowWindow(SW_HIDE);

		//chequeo formato de campo
		vpView->ChequeoFormato();

		break;
	}
	case VK_ESCAPE:	//restaura datos
	{
		CString texto;
		pgrilla->GetWindowText(texto);
		SetWindowText(texto);
		pgrilla->OnChangeEdit();
		ShowWindow(SW_HIDE);
		pgrilla->SetFocus();
		break;
	}

	}
	
	CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
	m_bBlock = false;
}

void CEditWnd::OnChange() 
{
	CMDIFrameWnd *pFrame = 
				 (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

	// Get the active MDI child window.
	CMDIChildWnd *pChild = 
				 (CMDIChildWnd *) pFrame->GetActiveFrame();

	// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

	// Get the active view attached to the active MDI child
	// window.
	vpView = (CIsismarcView *) pChild->GetActiveView();

	CString tex;


	if (hubo_sub)
	{
		vpView->m_ayuda.SetCellAlignment(alignment);
		vpView->m_ayuda.SetCellForeColor(color);
		vpView->m_ayuda.SetCellFontBold(bold);
		vpView->m_ayuda.SetCellFontSize(tam);
		vpView->m_ayuda.SetTextMatrix(0, 0, texto);
		hubo_sub = false;
	}


	GetWindowText(tex);
	int pos;
	if ( (pos = tex.Find("^")) >= 0 && !vpView->m_flexgrid1.editandopadre)
	{
		alignment = vpView->m_ayuda.GetCellAlignment();
		color = vpView->m_ayuda.GetCellForeColor();
		bold = vpView->m_ayuda.GetCellFontBold();
		tam = vpView->m_ayuda.GetCellFontSize();
		texto = vpView->m_ayuda.GetText();

		vpView->m_ayuda.SetCellAlignment(4);
		vpView->m_ayuda.SetCellForeColor(ROJO);
		vpView->m_ayuda.SetCellFontBold(true);
		vpView->m_ayuda.SetCellFontSize(12);
		
		vpView->m_ayuda.SetTextMatrix(0 , 0, tMensaje[37][0]);

		SetWindowText(tex.Left(pos) + tex.Mid(pos + 1, tex.GetLength() - pos ) );
		SetSel(pos, pos);

		hubo_sub = true;
	}

	if (!vpView->en_log)
	{
		//array con operaciones para el log
		CTime ahora = CTime::GetCurrentTime();
		
		char aux[80];
		strcpy(aux, "[");
		strcat( aux, vpView->base_activa.GetBuffer(vpView->base_activa.GetLength()) );
		vpView->base_activa.ReleaseBuffer();
		strcat(aux, "];");
		vpView->l_grabar.base = aux;		
		
		CString saux;
		saux.Format("%.4d%.2d%.2d %.2d%.2d%.2d", ahora.GetYear(), ahora.GetMonth(), 
			ahora.GetDay(), ahora.GetHour(), ahora.GetMinute(), ahora.GetSecond());
		strcpy (aux, saux.GetBuffer(saux.GetLength()) );
		saux.ReleaseBuffer();
		vpView->l_grabar.hora = aux;
		
		saux = "Updated";
		strcpy(aux, "[");
		strcat (aux, saux.GetBuffer(saux.GetLength()) );
		saux.ReleaseBuffer();
		strcat(aux, "];");
		vpView->l_grabar.operac = aux;

		saux.Format("%d", vpView->reg_actual);
		strcpy(aux, "[");
		strcat (aux, saux.GetBuffer(saux.GetLength()) );
		saux.ReleaseBuffer();
		strcat(aux, "];");
		vpView->l_grabar.registro = aux;

		saux = "user";
		strcpy(aux, "[");
		strcat (aux, saux.GetBuffer(saux.GetLength()) );
		saux.ReleaseBuffer();
		strcat(aux, "];");
		vpView->l_grabar.usuario = aux;

		vpView->op_a_grabar.Add(vpView->l_grabar);

		vpView->en_log = true;
	}


	if ( m_bBlock )
		return;
	m_bBlock = true;
	pgrilla->OnChangeEdit();
	m_bBlock = false;
}



void CEditWnd::OnKillfocus() 
{
	CMDIFrameWnd *pFrame = 
				 (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

	// Get the active MDI child window.
	CMDIChildWnd *pChild = 
				 (CMDIChildWnd *) pFrame->GetActiveFrame();

	// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

	// Get the active view attached to the active MDI child
	// window.
	vpView = (CIsismarcView *) pChild->GetActiveView();
	if (!vpView)
		return;

	int row = pgrilla->GetRow();
	if ( !m_bBlock )
	{
		CString cStr;
		GetWindowText(cStr);
		pgrilla->SetText(cStr);
		pgrilla->OnKillEdit();
		ShowWindow(SW_HIDE);
	}

	vpView->m_flexgrid1.SetScrollBars(3);	//restauro scrollbars
}




BOOL CEditWnd::PreTranslateMessage(MSG* pMsg)
{
	CFrameWnd *pFrame = (CFrameWnd *)AfxGetMainWnd();
    if ( TranslateAccelerator(pFrame->m_hWnd, pFrame->m_hAccelTable, pMsg) )
		return true;
    return CEdit::PreTranslateMessage(pMsg);
}


